package NetWork.UDPDome002;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

/**
 * ClassName:ReceiveMessage
 * Package:NetWork.UDPDome002
 * Description:
 *
 * @Author:ZhangXin
 * @Create:2023/3/7-20:44
 * @Version:v1.0
 */
public class ReceiveMessage {
    public static void main(String[] args) throws IOException {
        DatagramSocket ds = new DatagramSocket(10086);

        //接收数据包
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes,bytes.length);


        while (true) {
            ds.receive(dp);

            byte[] data = dp.getData();
            int length = dp.getLength();
            String ip = dp.getAddress().getHostAddress();
            String hostName = dp.getAddress().getHostName();

            System.out.println("ip为："+ip+"，主机名为："+hostName+"的人发送了数据："+new String(data,0,length));
        }

    }
}
